import {
	validateWaitTime,
	validateTimesMax,
	validateResultDataType,
	validateConfidence,
	validateInformationRushurlAddress,
	validateLink,
	validateVideoLink,
	validateWordage,
	validateShortVideoCollectTime,
	validateRequestApiWaitTime,
	validateInformationPushUrl
} from './utils';
import AIWorkflow from './workFlow';
// http://192.168.31.91:8090/pages/viewpage.action?pageId=1442724
export const aiWorkflowRules = {
	// AI播报话术 - 信息确认/信息记录/人脸对比/扫码识别/位置信息
	aiBroadcast: {
		required: true,
		minlength: 1,
		maxlength: 140,
		type: String
	},
	// 等待时长 - 信息确认/信息记录/播报模板/扫码识别/位置信息
	waitTime: {
		required: true,
		min: 0,
		max: 9999999,
		minlength: 1,
		maxlength: 7,
		type: Number
	},
	// 回答 - 信息确认/信息记录/人脸对比/活体检测/扫码识别/位置信息
	aiAnswer: {
		required: true,
		minlength: 1,
		maxlength: 280,
		type: String
	},
	// AI说 - 信息确认/信息记录/人脸对比/活体检测/扫码识别/位置信息
	aiTalk: {
		required: false,
		minlength: 1,
		maxlength: 140,
		type: String
	},
	// AI回复反问后，主流程提示话术
	aiProcessSpeech: {
		required: true,
		minlength: 1,
		maxlength: 200,
		type: String
	},
	// 最大允许次数 - 信息确认/信息记录/扫码识别/位置信息
	maxTalkTimes: {
		required: true,
		min: -1,
		max: 99,
		minlength: 1,
		maxlength: 2,
		type: Number
	},
	// 检测次数 - 人脸比对
	detectTimes: {
		required: true,
		min: 0,
		max: 99,
		type: Number
	},
	// 重试次数 - 活体检测
	repeatTimes: {
		required: true,
		min: 0,
		max: 99,
		type: Number
	},
	// 阈值 - 活体检测
	confidence: {
		required: true,
		min: 0,
		max: 100,
		type: Number
	},
	// 等待时长 - 表单填写/静默等待模块
	waitOverTime: {
		required: true,
		min: 0,
		max: 9999999,
		minlength: 1,
		maxlength: 7,
		type: Number
	},
	// AI播报话术 ai 说
	aiWords: {
		required: true,
		minlength: 1,
		maxlength: 1000,
		type: String
	},
	// 视频采集时长等待时长 - 短视频采集
	shortVideoTime: {
		required: true,
		min: 0,
		max: 999999, // 600000
		minlength: 1,
		maxlength: 6,
		type: Number
	}
};

const customizeValidateReuqiredVideo = (rule, value, callback) => {
	if (!value && !AIWorkflow.cloneForm.aiSpeakVideo) {
		AIWorkflow.cloneForm.aiSpeakVideoValidate = false;
		console.log(AIWorkflow.cloneForm);
		return callback(new Error('请输入AI客服播报的话术及选择播放视频'));
	}
	if (!AIWorkflow.cloneForm.aiSpeakVideo) {
		AIWorkflow.cloneForm.aiSpeakVideoValidate = false;
		return callback(new Error('请选择播放视频'));
	}
	if (!value) {
		AIWorkflow.cloneForm.aiSpeakVideoValidate = true;
		return callback(new Error('请输入AI客服播报的话术'));
	}
	AIWorkflow.cloneForm.aiSpeakVideoValidate = true;
	return callback();
};
/** 验证预录制，视频原生Video必填项 */
const customizeValidatePrerecordedNativeVideo = (rule, value, callback) => {
	if (!AIWorkflow.cloneForm.aiSpeakVideo) {
		AIWorkflow.cloneForm.aiSpeakVideoValidate = false;
		return callback(new Error('请选择播放视频'));
	}
	AIWorkflow.cloneForm.aiSpeakVideoValidate = true;
	return callback();
};

const customizeValidateVideo = (rule, value, callback) => {
	if (!value && !AIWorkflow.cloneForm[`${rule.field}Video`]) {
		AIWorkflow.cloneForm[`${rule.field}VideoValidate`] = true;
		return callback();
	}
	if (value && !AIWorkflow.cloneForm[`${rule.field}Video`]) {
		AIWorkflow.cloneForm[`${rule.field}VideoValidate`] = false;
		return callback(new Error('请选择播放视频'));
	}
	AIWorkflow.cloneForm[`${rule.field}VideoValidate`] = true;
	return callback();
};

const validateSpeakVideo = (rule, value, callback) => {
	if (!AIWorkflow.cloneForm[`${rule.field}Video`]) {
		AIWorkflow.cloneForm[`${rule.field}VideoIcon`] = AIWorkflow.cloneForm.errorVideo;
		return callback(new Error('请选择播放视频'));
	}
	AIWorkflow.cloneForm[`${rule.field}VideoIcon`] = require('@bpmn/assets/img/aivideo.png');
	return callback();
};

const ivvrOptionsValidateVideo = (rule, value, callback) => {
	const errorVideo = require('@bpmn/assets/img/aivideoError.png');
	const videoIcon = require('@bpmn/assets/img/aivideo.png');
	AIWorkflow.cloneForm.ivvrOptions.forEach(item => {
		if (item.answerSpeak && !item.video) {
			item.icon = errorVideo;
			return callback(new Error('请选择播放视频'));
		}
		console.log(AIWorkflow.cloneForm.ivvrOptions);
		item.icon = videoIcon;
		return callback();
	});

	return callback();
};

/** 信息汇总校验 */
const validateSummary = (rule, value, callback) => {
	console.log('validateSummary', rule, value);
	if (value.length === 0) {
		return callback(new Error('请选择节点内容'));
	}
	return callback();
};

const hasConfidence = ['detectLivingFace', 'faceCompare', 'idCardCompare', 'interfaceCall'];

export const getTemplateReules = (talkId, property, form) => {
	const rules = {
		aiSpeak: [
			{
				required: aiWorkflowRules.aiBroadcast.required,
				message: '请输入AI客服播报的话术',
				trigger: ['blur', 'change']
			}
		],
		aiSpeakVideo: [{ validator: customizeValidatePrerecordedNativeVideo, form, trigger: ['blur', 'change'] }],
		cardCollectionType: [
			{
				required: true,
				message: '请选择卡证类型',
				trigger: ['change', 'blur']
			}
		],
		waitTime: [
			{
				required: true,
				message: talkId === 'interfaceCall' ? '请输入调取接口的等待时长' : '请输入等待用户回答时间（毫秒）',
				trigger: ['blur', 'change']
			},
			{ validator: talkId === 'interfaceCall' ? validateRequestApiWaitTime : validateWaitTime, trigger: ['blur'] }
		],
		aiProcessSpeech: [
			{
				required: aiWorkflowRules.aiProcessSpeech.required,
				message: '请输入主流程提示话术',
				trigger: ['blur']
			},
			{
				min: aiWorkflowRules.aiProcessSpeech.minlength,
				max: aiWorkflowRules.aiProcessSpeech.maxlength,
				message: '长度在 1 到 200 个字符',
				trigger: ['blur']
			}
		],
		wordsYes: [
			{
				required: aiWorkflowRules.aiTalk.required,
				message: 'AI客服播报的话术',
				trigger: ['blur']
			},
			{
				min: aiWorkflowRules.aiTalk.minlength,
				max: aiWorkflowRules.aiTalk.maxlength,
				message: `长度在 ${aiWorkflowRules.aiTalk.minlength} 到 ${aiWorkflowRules.aiTalk.maxlength} 个字符`,
				trigger: ['blur']
			}
		],
		nextStepYes: [
			{
				required: true,
				message: '请选择下一步操作',
				trigger: ['change', 'blur']
			}
		],
		wordsNo: [
			{
				required: aiWorkflowRules.aiTalk.required,
				message: '请输入AI客服播报的话术',
				trigger: ['blur']
			},
			{
				min: aiWorkflowRules.aiTalk.minlength,
				max: aiWorkflowRules.aiTalk.maxlength,
				message: `长度在 ${aiWorkflowRules.aiTalk.minlength} 到 ${aiWorkflowRules.aiTalk.maxlength} 个字符`,
				trigger: ['blur']
			}
		],
		nextStepNo: [
			{
				required: true,
				message: '请选择下一步操作',
				trigger: ['blur']
			}
		],
		resultFailedNextStep: [
			{
				required: true,
				message: '请选择下一步操作',
				trigger: ['blur']
			}
		],
		wordsOther: [
			{
				required: aiWorkflowRules.aiTalk.required,
				message: '请输入AI客服播报的话术',
				trigger: ['blur']
			},
			{
				min: aiWorkflowRules.aiTalk.minlength,
				max: aiWorkflowRules.aiTalk.maxlength,
				message: `长度在 ${aiWorkflowRules.aiTalk.minlength} 到 ${aiWorkflowRules.aiTalk.maxlength} 个字符`,
				trigger: ['blur']
			}
		],
		timesMax: [
			{
				required: aiWorkflowRules.maxTalkTimes,
				message: '请输入允许用户其他回答的最大次数',
				trigger: ['blur', 'change']
			},
			{ validator: validateTimesMax, trigger: ['blur'] }
		],
		nextStepOther: [
			{
				required: true,
				message: '请选择下一步操作',
				trigger: ['blur']
			}
		],
		wordsEnd: [
			{
				required: aiWorkflowRules.aiTalk.required,
				message: '请输入下一步操作前，AI播报的话术',
				trigger: ['blur']
			},
			{
				min: aiWorkflowRules.aiTalk.minlength,
				max: aiWorkflowRules.aiTalk.maxlength,
				message: `长度在 ${aiWorkflowRules.aiTalk.minlength} 到 ${aiWorkflowRules.aiTalk.maxlength} 个字符`,
				trigger: ['blur']
			}
		],
		wordsNoAnswer: [
			{
				required: aiWorkflowRules.aiTalk.required,
				message: '请输入AI客服播报的话术',
				trigger: ['blur']
			},
			{
				min: aiWorkflowRules.aiTalk.minlength,
				max: aiWorkflowRules.aiTalk.maxlength,
				message: `长度在 ${aiWorkflowRules.aiTalk.minlength} 到 ${aiWorkflowRules.aiTalk.maxlength} 个字符`,
				trigger: ['blur']
			}
		],

		noAnswerTimesMax: [
			{
				required: aiWorkflowRules.maxTalkTimes,
				message: '请输入允许用户不回答的最大次数',
				trigger: ['blur', 'change']
			},
			{ validator: validateTimesMax, trigger: ['blur'] }
		],
		resultFailedTimesMax: [
			{
				required: aiWorkflowRules.maxTalkTimes,
				message: '请输入允许用户识别的最大次数',
				trigger: ['blur', 'change']
			},
			{ validator: validateTimesMax, trigger: ['blur'] }
		],
		llmRecognizeTimesMax: [
			{
				required: aiWorkflowRules.maxTalkTimes,
				message: '请输入允许用户识别的最大次数',
				trigger: ['blur', 'change']
			},
			{ validator: validateTimesMax, trigger: ['blur'] }
		],
		otherTimesMax: [
			{
				required: hasConfidence.indexOf(talkId) === -1 ? aiWorkflowRules.maxTalkTimes : aiWorkflowRules.detectTimes,
				message: hasConfidence.indexOf(talkId) === -1 ? '请输入允许用户其他回答的最大次数' : talkId === 'interfaceCall' ? '请输入允许调用失败的最大次数' : '请输入最大允许检测的次数',
				trigger: ['blur', 'change']
			},
			{ validator: validateTimesMax, trigger: ['blur'] }
		],
		nextStepNoAnswer: [
			{
				required: true,
				message: '请选择下一步操作',
				trigger: ['blur']
			}
		],

		llmRecognizeNextStep: [
			{
				required: true,
				message: '请选择下一步操作',
				trigger: ['blur']
			}
		],

		transferReason: [
			{
				required: true,
				message: '请输入转人工原因',
				trigger: ['blur']
			}
		],
		wordsEndNoAnswer: [
			{
				required: aiWorkflowRules.aiTalk.required,
				message: '请输入下一步操作前，AI播报的话术',
				trigger: ['blur']
			},
			{
				min: aiWorkflowRules.aiTalk.minlength,
				max: aiWorkflowRules.aiTalk.maxlength,
				message: `长度在 ${aiWorkflowRules.aiTalk.minlength} 到 ${aiWorkflowRules.aiTalk.maxlength} 个字符`,
				trigger: ['blur']
			}
		],
		collectionResultDataType: [
			{
				required: true,
				message: '请选择收集结果数据类型',
				trigger: 'blur'
			},
			{ validator: validateResultDataType, form, trigger: 'blur' }
		],
		confidence: [
			{
				required: aiWorkflowRules.confidence.required,
				message: '请输入阈值 [0,100]',
				trigger: 'blur'
			},
			{ validator: validateConfidence, trigger: 'blur' }
		],
		formId: [
			{
				required: true,
				message: '请选择表单',
				trigger: ['change', 'blur']
			}
		],
		typeOfCollectedResults: [
			{
				required: true,
				message: '请选择收集结果数据类型',
				trigger: ['change', 'blur']
			},
			{ validator: validateResultDataType, form, trigger: 'blur' }
		],
		testimonyId: [
			{
				required: true,
				message: '请选择抄录证言',
				trigger: ['blur']
			}
		],
		wordsVariable: [{ required: true, message: '请选择要收集的变量', trigger: ['change', 'blur'] }],
		bizSceneId: [{ required: true, message: '请选择目标场景ID', trigger: ['blur'] }],
		informationRushurlAddress: [
			{
				required: talkId === 'interfaceCall' ? true : false,
				message: '请输入URL地址',
				trigger: ['blur', 'change']
			},
			{ validator: talkId === 'interfaceCall' ? validateInformationRushurlAddress : validateInformationPushUrl, trigger: 'blur' }
		],
		hangupReason: [
			{ required: true, message: '请输入挂断原因', trigger: 'blur' },
			{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
		],
		wordsOverTime: [{ required: true, message: '请输入等待用户回答时间（毫秒）', trigger: ['blur', 'change'] }],
		noAnswerOverTime: [{ required: true, message: '请输入等待用户回答时间（毫秒）', trigger: ['blur', 'change'] }],
		intentLib: [{ required: true, message: '请选择意图库', trigger: 'blur' }],

		ivvrOptions: {
			nextStepsAnswer: [
				{
					required: true,
					message: '请选择下一步操作',
					trigger: 'change'
				}
			],
			answerIntentionSelect: [
				{
					required: true,
					message: '请选择意图点',
					trigger: ['blur', 'change']
				}
			],
			answerIntention: [
				{
					required: true,
					message: '请输入意图点',
					trigger: ['blur', 'change']
				}
			],
			answerSpeak: []
		},
		judgeOptions: {
			option: [
				{
					required: true,
					message: '请编辑结果选项',
					trigger: ['blur', 'change']
				}
			],
			nextStepOption: [
				{
					required: true,
					message: '请选择下一步操作',
					trigger: 'blur'
				}
			]
		},
		summary: [
			{
				required: true,
				message: '请选择节点内容',
				trigger: ['blur', 'change']
			},
			{ validator: validateSummary, form, trigger: ['blur', 'change'] }
		],
		wordsSendContents: {
			type: [{ required: true, message: '请选择相关业务', trigger: ['change', 'blur'] }],
			url: [
				{ required: true, message: '请输入推送链接地址', trigger: ['change', 'blur'] },
				{ validator: validateLink, trigger: ['blur'] }
			],
			webUrl: [
				{ required: true, message: '请输入推送链接地址', trigger: ['change', 'blur'] },
				{ validator: validateLink, trigger: ['blur'] }
			],
			inputUrl: [{ validator: validateVideoLink, form, trigger: ['change', 'blur'] }],
			next: [{ required: true, message: '请选择下一步操作', trigger: ['change', 'blur'] }],
			wordage: [
				{ required: true, message: '请输入文本内容', trigger: ['change', 'blur'] },
				{
					validator: validateWordage,
					form,
					trigger: ['change', 'blur']
				}
			],
			uploadPdfUrl: [{ required: true, message: '您还未选择PDF文件！', trigger: ['change', 'blur'] }],
			uploadUrl: [{ required: true, message: '您还未选择视频文件！', trigger: ['change', 'blur'] }]
		},
		nlpOptions: {
			nlpBack: [
				{
					required: true,
					message: '请至少选择一个NLP返回标识',
					trigger: ['blur', 'change']
				}
			],
			nextStepsNlp: [
				{
					required: true,
					message: '请选择下一步操作',
					trigger: 'change'
				}
			]
		},
		variableJudgeOptions: {
			variableName: [
				{
					required: true,

					message: ' ',
					trigger: 'blur'
				}
			],
			rule: [
				{
					required: true,

					message: ' ',
					trigger: 'blur'
				}
			],
			value: [
				{
					required: true,

					message: ' ',
					trigger: 'blur'
				}
			],
			nextStepsAnswer: [
				{
					required: true,
					message: '请选择下一步操作',
					trigger: 'change'
				}
			],
			conditions: [
				{
					required: true,
					message: '请选择下一步操作'
				}
			]
		},
		businessInstruct: {
			type: [{ required: true, message: '请选择相关业务', trigger: 'blur' }]
		},
		videoCollectTime: [
			{
				required: aiWorkflowRules.waitTime.required,
				message: '请选择采集时长',
				trigger: ['change']
			},
			{ validator: validateWaitTime, trigger: ['blur'] }
		],
		talkMode: [
			{
				required: aiWorkflowRules.waitTime.required,
				message: '请选择通话模式',
				trigger: ['change']
			},
			{ validator: validateWaitTime, trigger: ['blur'] }
		],
		microphoneState: [
			{
				required: aiWorkflowRules.waitTime.required,
				message: '请选择麦克风状态',
				trigger: ['change']
			},
			{ validator: validateWaitTime, trigger: ['blur'] }
		],
		collectShortVideoTime: [
			{
				required: aiWorkflowRules.waitTime.required,
				message: '请选择采集时长',
				trigger: ['change']
			},
			{ validator: validateShortVideoCollectTime, trigger: ['blur'] }
		],
		inviteMediaType: [
			{
				required: aiWorkflowRules.waitTime.required,
				message: '请选择通话模式',
				trigger: ['change']
			},
			{ validator: validateWaitTime, trigger: ['blur'] }
		],
		microphoneStatus: [
			{
				required: aiWorkflowRules.waitTime.required,
				message: '请选择麦克风状态',
				trigger: ['change']
			},
			{ validator: validateWaitTime, trigger: ['blur'] }
		],
		callMethod: [
			{
				required: aiWorkflowRules.waitTime.required,
				message: '请选择调用方式',
				trigger: ['change']
			},
			{ validator: validateWaitTime, trigger: ['blur'] }
		]
	};
	const { videoType, audioType } = property;
	if (videoType === 'pre-recorded-video' && audioType === 'tts') {
		rules['aiSpeak'] = [{ validator: talkId === 'messageCollect' ? customizeValidateVideo : customizeValidateReuqiredVideo, form, trigger: ['change', 'blur'] }];
		rules['wordsYes'] = [{ validator: customizeValidateVideo, form, trigger: ['blur', 'change'] }];
		rules['wordsNo'] = [{ validator: customizeValidateVideo, form, trigger: ['blur', 'change'] }];
		rules['wordsOther'] = [{ validator: customizeValidateVideo, form, trigger: ['blur', 'change'] }];
		rules['wordsEnd'] = [{ validator: customizeValidateVideo, form, trigger: ['blur', 'change'] }];
		rules['wordsNoAnswer'] = [{ validator: customizeValidateVideo, form, trigger: ['blur', 'change'] }];
		rules['wordsEndNoAnswer'] = [{ validator: customizeValidateVideo, form, trigger: ['blur', 'change'] }];
		rules['resultFailedSpeak'] = [{ validator: customizeValidateVideo, form, trigger: ['blur', 'change'] }];
		rules['resultFailedNextStepSpeak'] = [{ validator: customizeValidateVideo, form, trigger: ['blur', 'change'] }];
		rules['ivvrOptions']['answerSpeak'] = [{ validator: ivvrOptionsValidateVideo, form, trigger: ['change', 'blur'] }];
	}
	if (videoType === 'pre-recorded-video' && audioType === 'video-native') {
		rules['aiSpeak'] = [{ validator: validateSpeakVideo, form, trigger: ['blur'] }];
	}
	return rules;
};
